/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';
import tpl from './tpl.html';
import commonService from '../../../common/commonService';
import service from './service'
import filter from '../../../../grsc-base/util/filter';
import lib from '../../../common/lib';
import format from '../../../common/format';

import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import MathTool from '../../../../grsc-base/util/mathtool';
import AddAlipay from '../../../component/addAlipay/addAlipay';
import AddBankCard from '../../../component/addBankCard/addBankCard';

import {PAY_TYPE_BANK, PAY_TYPE_ALIPAY} from '../../../common/contants';

export default Vue.extend({
    name: "C2Cset",
    template: tpl,
    components: {
        Loading,
        NoData,
        AddAlipay,
        AddBankCard
    },

    data() {
        return {
            format,
            lib: lib,
            isLoading: false,
            alipayInfo: null,
            bankInfo: null,

            isShowAlipay: false,
            isShowBank: false
        }
    },
    created() {
        this._fetchPayInfos();
    },
    mounted(){
    },
    methods: {
        _fetchPayInfos() {
            this.isLoading = true;
            service.getPayInfos().done((rep) => {
                this.isLoading = false;

                if (rep.code === window.CODE.SUCCESS) {
                    let list = rep.data;
                    if (list.length !== 0) {
                        $.map(list, (item, index) => {
                            if (item.c2cPayType === PAY_TYPE_ALIPAY && item.name) {
                                this.alipayInfo = item;
                            }
                            if (item.c2cPayType === PAY_TYPE_BANK && item.name) {
                                this.bankInfo = item;
                            }
                        });
                    }
                }
            });
        },

        /**
         * 支付寶
         */
        doAlipay(doType) {
            this.doType = doType;
            this.isShowAlipay = true;
        },

        /**
         * 銀行卡
         */
        doBank(doType) {
            this.doType = doType;
            this.isShowBank = true;
        },

        /**
         * 添加支付寶成功
         */
        onAddAlipaySuccess() {
            this.isShowBank = false;
            this._fetchPayInfos();
        },

        /**
         * 添加銀行卡成功
         */
        onAddBankSuccess() {
            this.isShowBank = false;
            this._fetchPayInfos();
        }

    },
    watch: {}
});
